package calorie.com.calorie.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import com.lxh.baselibray.dialog.AlertDialog;
import com.lxh.baselibray.event.BusFactory;
import com.lxh.baselibray.mvp.MvpActivity;
import com.lxh.baselibray.util.Md5Utils;
import com.lxh.baselibray.util.SPUtils;
import com.lxh.baselibray.util.SizeUtils;
import com.lxh.baselibray.util.ToastUtils;
import com.lxh.baselibray.view.PswText;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import calorie.com.calorie.R;
import calorie.com.calorie.constant.Constant;
import calorie.com.calorie.contact.SureOrderContact;
import calorie.com.calorie.eventbus.EventImpl;
import calorie.com.calorie.response.SureOrderResponce;
import retrofit2.Response;
//待确认   可以确认订单
public class MallWaitSureDetailActivity extends MvpActivity<SureOrderContact.SureOrderPresent> implements SureOrderContact.ISureOrderView {

    @BindView(R.id.goods_detail_name)
    TextView goodsDetailName;
    @BindView(R.id.goods_detail_price)
    TextView goodsDetailPrice;
    @BindView(R.id.goods_detail_no)
    TextView goodsDetailNo;
    @BindView(R.id.the_goods_name)
    TextView theGoodsName;
    @BindView(R.id.the_goods_phone)
    TextView theGoodsPhone;
    @BindView(R.id.the_goods_address)
    TextView theGoodsAddress;
    @BindView(R.id.the_goods_create_time)
    TextView theGoodsCreateTime;
    @BindView(R.id.sure_btn)
    TextView sureBtn;

    String goodsName, goodsPrice, orderId, receiveName, receivePhone, receiveAdreess, orderCreateTime;

    @Override
    public void initData(Bundle savedInstanceState) {
        Intent intent = getIntent();
        if (intent != null) {
            goodsName = intent.getStringExtra("goodsName");
            goodsPrice = intent.getStringExtra("goodsPrice");
            orderId = intent.getStringExtra("orderId");
            receiveName = intent.getStringExtra("shippedName");
            receivePhone = intent.getStringExtra("shippedPhone");
            receiveAdreess = intent.getStringExtra("shippedAddress");
            orderCreateTime = intent.getStringExtra("goodsCreateTime");
            initDetail();
        }
    }

    private void initDetail() {
        goodsDetailName.setText(goodsName);
        goodsDetailPrice.setText(goodsPrice);
        goodsDetailNo.setText(orderId);
        theGoodsName.setText(receiveName);
        theGoodsPhone.setText(receivePhone);
        theGoodsAddress.setText(receiveAdreess);
        theGoodsCreateTime.setText(orderCreateTime);

    }
    @Override
    public int getLayoutId() {
        return R.layout.activity_mall_wait_sure_detail;
    }

    @Override
    protected SureOrderContact.SureOrderPresent createPresent() {
        return new SureOrderContact.SureOrderPresent();
    }

    @OnClick(R.id.sure_btn)
    public void onViewClicked() {
        int userId = SPUtils.getInt(Constant.USER_ID,-1,context);
        String orderId = goodsDetailNo.getText().toString();
        showDealDialog(userId,orderId);
    }

    AlertDialog dealDialog = null;

    private void showDealDialog(Integer userId, String orderId) {//交易密码弹窗
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .addDefaultAnimation()
                .setCancelable(true)
                .setContentView(R.layout.dialog_deal_pwd)
                .setWidthAndHeight(SizeUtils.dp2px(context, 279), SizeUtils.dp2px(context, 157))
                .setOnClickListener(R.id.tv_sure, v -> {
                    PswText psw = dealDialog.getView(R.id.pt_deal_pwd);
                    String dealPwd = Md5Utils.stringToMD5(psw.getPsw());
                    mPresent.sureOrders(context,orderId, userId, dealPwd);
                    dealDialog.dismiss();
                }).setOnClickListener(R.id.tv_cancle, v -> dealDialog.dismiss());
        dealDialog = builder.create();
        dealDialog.show();
    }
    @Override
    public void getSureOrderResult(Response<SureOrderResponce> response) {
        if (response.body().getCode() == Constant.SUCCESS_CODE){
            ToastUtils.showLongToast(context,"已收货！");
            BusFactory.getBus().post(new EventImpl.SureOrderEvent());
            finish();
        }else {
            ToastUtils.showLongToast(context,response.body().getMsg());
        }
    }
}
